
package com.yimixian.app.user;

import com.yimixian.app.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class SettingsView extends LinearLayout {

    public interface Adapter {
        int getCount();

        SettingsItem getSettingsItem(int position);
    }

    public SettingsView(Context context) {
        super(context);
        initSettingsView();
    }

    public SettingsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSettingsView();
    }

    public SettingsView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initSettingsView();
    }

    private void initSettingsView() {
        setBackgroundColor(getResources().getColor(R.color.background_grey));
        setOrientation(VERTICAL);
    }

    public void setAdapter(Adapter adapter) {
        removeAllViews();
        if (adapter == null) {
            return;
        }

        for (int i = 0; i < adapter.getCount(); ++i) {
            SettingsItem item = adapter.getSettingsItem(i);
            SettingsItemView itemView = new SettingsItemView(getContext());
            itemView.setDrawDividerBelow(i != adapter.getCount() - 1 && !item.mAddGroupDividerAfter);
            itemView.setStandardViews(item.mIconResId, item.mTitle, item.mShowRightArrow,
                    item.mShowBadge, item.mLinkInfo);
            LayoutParams params = new LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(
                            R.dimen.settings_item_height));
            if (i == 0 || adapter.getSettingsItem(i - 1).mAddGroupDividerAfter) {
                params.setMargins(0,
                        getResources()
                                .getDimensionPixelSize(R.dimen.settings_item_group_top_margin), 0,
                        0);
            }
            itemView.setLayoutParams(params);
            itemView.setOnClickListener(item.mOnClickListener);
            itemView.setClickable(item.mOnClickListener != null);
            addView(itemView);
        }
    }
}
